package cn.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.NewsDao;
import entity.News;

public class AdminServlet extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int totalPages = NewsDao.Dao.newsConut(null);
		int pages = (totalPages / 22) + ((totalPages % 22) == 0 ? 0 : 1);
		int pageNo = 1;
		String s = request.getParameter("pageNo");
		try {
			pageNo = Integer.parseInt(s);
		} catch (Exception e) {
			// TODO: handle exception
		}
		List<News> newsPage = NewsDao.Dao.showAdminNews(pageNo, null);
		request.setAttribute("newsPage", newsPage);
		request.setAttribute("pageNo", pageNo);
		request.setAttribute("totalPages", pages);
		request.getRequestDispatcher("/WEB-INF/jsp/admin.jsp").forward(request,
				response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
